package com.one.common.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.one.common.R;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/10/27 8:53 PM
 * description ：订单进度view
 **/
public class StepView extends LinearLayout {

    private int normalLineColor, normalTextColor, normalImg;
    private int checkLineColor, checkTextColor, checkImg;
    private String[] data;

    public StepView(Context context) {
        super(context, null);
    }

    public StepView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    /**
     * 初始化
     */
    private void init(Context context, AttributeSet attrs) {
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.StepViewAttrs);
        normalLineColor = ta.getResourceId(R.styleable.StepViewAttrs_sv_normal_line_color, -1);
        normalTextColor = ta.getResourceId(R.styleable.StepViewAttrs_sv_normal_text_color, -1);
        normalImg = ta.getResourceId(R.styleable.StepViewAttrs_sv_normal_img, -1);
        checkLineColor = ta.getResourceId(R.styleable.StepViewAttrs_sv_check_line_color, -1);
        checkTextColor = ta.getResourceId(R.styleable.StepViewAttrs_sv_check_text_color, -1);
        checkImg = ta.getResourceId(R.styleable.StepViewAttrs_sv_check_img, -1);
        ta.recycle();
    }


    public void setData(String[] data) {
        this.data = data;
        checkIndex(0);
    }

    /**
     * 设置指定进度的显示
     */
    public void checkIndex(int index) {
        this.removeAllViews();
        if (data != null && data.length > 0) {
            for (int i = 0; i < data.length; i++) {
                StepItem item = new StepItem(getContext());
                if (i <= index) {
                    item.setImage(checkImg);
                    item.setLineColor(checkLineColor);
                    item.setTextColor(checkTextColor);
                } else {
                    item.setImage(normalImg);
                    item.setLineColor(normalLineColor);
                    item.setTextColor(normalTextColor);
                }
                item.setText(data[i]);
                addView(item);
                LayoutParams layoutParams = (LayoutParams) item.getLayoutParams();
                layoutParams.weight = 1;
                item.setLayoutParams(layoutParams);
            }
        }
    }


}
